<div class="layui-fluid layui-anim febs-anim" id="businesses-address" lay-title="现场确认地">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="address-table-form">

                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                            <legend style="font-size: 15px; font-weight: bolder;">批量查询</legend>
                        </fieldset>
                        <div class="layui-row">

                            <!--批量查询条件-->
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline" style="padding-left: 50px">
                                        <select name="province" id="province_add" lay-filter="province_add">
                                            <option value="">请选择省</option>
                                        </select>
                                    </div>
                                    <div class="layui-inline" style="padding-left: 50px">
                                        <select name="city" id="city_add" lay-filter="city_add">
                                            <option value="">请选择市</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>   <!--查询符号-->
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i> <!--重置符号-->
                                </div>
                            </div>
                        </div>
                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            <div class="layui-row">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action" id="add">
                                    <i class="layui-icon layui-icon-plus-circle">新增</i> <!--新增按钮-->
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action" id="export">
                                    <i class="layui-icon layui-icon-export">导出</i><!--导出按钮-->
                                </div>
                            </div>
                        </blockquote>
                    </form>
                    <table lay-filter="addressTable" lay-data="{id: 'addressTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="address-option">
    <a lay-event="detail"><i
            class="layui-icon febs-edit-area febs-green">&#xe7a5;</i></a><!--详情-->
    <a lay-event="edit"><i
            class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a><!--编辑-->
    <a lay-event="del"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a><!--删除-->
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'form', 'table', 'febs'], function () {
        let $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#businesses-address'),
            $query = $view.find('#query'),
            $add = $view.find('#add'),
            $export = $view.find('#export'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createDate', type: null},
            tableIns;

        form.render();

        initTable();

        table.on('tool(addressTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                febs.modal.view('现场确认点信息', 'confirmAddress/detail/' + data.id, {
                    area: $(window).width() <= 750 ? '95%' : '660px'
                });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除确认地点', '确定删除该地点？', function () {
                    deleteAddress(data.id);
                });
            }
            if (layEvent === 'edit') {
                febs.modal.open('修改确认地点', 'confirmAddress/update/' + data.id, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#address-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        table.on('sort(addressTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $add.on('click', function () {
            febs.modal.open('新增确认地点', 'confirmAddress/add', {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                yes: function (index, layero) {
                    $('#address-add').find('#submit').trigger('click');
                    //防止重复提交
                    var DISABLED = 'layui-btn-disabled';
                    // 增加样式
                    $('#address-add').find('#submit').addClass(DISABLED);
                    // 增加属性
                    $('#address-add').find('#submit').attr('disabled', 'disabled');
                },
                btn2: function () {
                    $('#address-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });

//        导出excel文件
        $export.on('click',function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
            params.pageNum = $view.find(".layui-laypage-em").next().html();
            febs.download(ctx + 'confirmAddress/excel', params, '现场确认点表.xlsx');
        })

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            tableIns.reload({where: getQueryParams(true), page: {curr: 1}, initSort: sortObject});
        });
//        展示确认地点列表
        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'addressTable',
                url: ctx + 'confirmAddress/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'provinceName',     title: '省', minWidth: 100},
                    {field: 'cityName',         title: '市', minWidth: 100},
                    {field: 'areaName',         title: '区'},
                    {field: 'specificLocation', title: '详细地址', minWidth: 165},
                    {field: 'status',           title: '状态', minWidth: 180, sort: true},
                    {title: '操作', toolbar: '#address-option', minWidth: 140}
                ]]
            });
        }

        function getQueryParams(flag) {
            let params = $searchForm.serializeJson();
            return params;
        }

        function deleteAddress(addressIds) {
            febs.get(ctx + 'confirmAddress/delete/' + addressIds, null, function () {
                febs.alert.success('删除地点成功');
                $query.click();
            });
        }

        //查询省份
        $.ajax({
            type:'get',
            url:'province',
            success:function(res){
                for(var i=0;i<res.data.length;i++){
                    let provinces = res.data[i];
                    $("#province_add").append(new Option(provinces.provinceName,provinces.id));
                }
                layui.form.render("select");
            }
        });

        //省市联动
        form.on('select(province_add)',function () {

            var pid = $("#province_add").val();
            $.ajax({
                type:'get',
                url:'city',
                data:{"pid":pid},
                success:function(res) {
                    var c=document.getElementById("city_add");
                    c.options.length=1;
                    for (var i = 0; i < res.data.length; i++) {
                        let citys = res.data[i];
                        $("#city_add").append(new Option(citys.cityName, citys.id));
                    }
                    layui.form.render("select");
                }
            })
        })

    })
</script>